package com.foreknow.controller;

import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;
import com.foreknow.util.Pagination;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ManagerServlet extends HttpServlet {
    private int pageSize = 10;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //所有的操作需要在doGet中完成
        //我们只需要在doget方法中获取请求参数，然后根据获取到的参数来决定调用哪一个方法
        String method = req.getParameter("q");
        if ("delete".equals(method)){
            delete(req,resp);
        }else  if ("update".equals(method)){
            update(req,resp);
        }else if ("selectById".equals(method)){
            selectById(req,resp);
        }
        else{
            list(req,resp);
        }
    }



    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        //获取到全局的初始化参数
        this.pageSize = Integer.parseInt(config.getServletContext().getInitParameter("pageSize"));
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有的留言信息
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        //调用业务层查询所有留言信息的方法
        GuestbookService service = new GuestbookServiceImpl();
        //要从页面中获取的页码
        int pageNumber = 0;
        try{
            pageNumber = Integer.parseInt(req.getParameter("pageNo"));
        }catch(Exception e){
            pageNumber = 1;
        }

        Pagination pagination = service.getByPage(pageSize,pageNumber);
        //获取区间数据
        MappingFactory mappingFactory = MappingFactory.getInstance();
        EntityMapping mapping = mappingFactory.getMap(MappingFactory.GUESTBOOK_MAPPING);
        List<Object> list = pagination.getList(mapping);

        //将查询到的list集合保存到服务器端
        //将总记录数保存到request中
        req.setAttribute("total",pagination.getMaxElements());
        //将查询到的list集合保存到服务器端
        req.setAttribute("list",list);
        //就可以将这个请求转发给list.jsp
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
    }



    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //删除留言的方法
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        GuestbookService service = new GuestbookServiceImpl();
        String id =req.getParameter("id");
        boolean isRight = service.deleteBean(Integer.parseInt(id));
        if (isRight){
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
        }


    }

    protected void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //修改留言的方法
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        GuestbookService service = new GuestbookServiceImpl();
        String id =req.getParameter("id");
        //调用service层修改留言方法
        Guestbook guestbook= service.updateBean(Integer.parseInt(id));
            //将查询到的Guestbook保存到服务器端
        req.setAttribute("guestbook",guestbook);
            req.getRequestDispatcher("/updateInfo.jsp").forward(req,resp);


    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {

        //删除留言的方法
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        GuestbookService service = new GuestbookServiceImpl();
        Guestbook guestbook = new Guestbook();
        String id= req.getParameter("id");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String eamil = req.getParameter("email");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        guestbook.setCreatedtime(simpleDateFormat.format(new Date()));
        guestbook.setName(name);
        guestbook.setPhone(phone);
        guestbook.setEmail(eamil);
        guestbook.setTitle(title);
        guestbook.setContent(content);
        guestbook.setId(Integer.parseInt(id));

        boolean isRight = service.updateByObj(guestbook);
        if (isRight){
            req.getRequestDispatcher("/user/manager?q=list").forward(req,resp);
        }
    }
}
